/**
 * author : abbe
 * email: 1418154909@qq.com
 *
 * */
;
! function(win, doc) {
    var dconfig = {
        v: "1.0.0",
        time: 3,
        animation: "fade",
        mask: true
    }
    var Dialog = null;
    var PushDialogId = [],
        defaultId = 19941205;
    var btnCallBack = {

    };

    function each(arr, fn) {
        if ("length" in arr) {
            for (var i = 0, len = arr.length; i < len; i++) {
                fn(i, arr[i]);
            }
        }
    }

    function init() {
        /** 对外展示的接口api **/
        var _that = this;
        /**收集按钮的回调**/
        return {
            version: dconfig.v,
            alert: function(msg, btnText, callBack) {
                // _that.close();
                return this.open({
                    content: "<div class='content_alert'>" + msg + "</div>",
                    btn: [btnText || "确定"],
                    btnFun: [callBack && function() {
                        return callBack(arguments);
                    }]
                }, "alert");
            },
            confirm: function(msg, callBack1, callBack2) {
                return this.open({
                    content: "<div class='content_confirm'>" + msg + "</div>",
                    btn: ["确定", "取消"],
                    btnFun: [callBack1 && function() {

                            return callBack1();
                        },
                        callBack2 && function() {
                            return callBack2();
                        }
                    ]
                }, "confirm");

            },
            prompt: function(msg, callBack1, callBack2) {
                return this.open({
                    content: "" + msg + "<div class='input'><input type='text' value='' placeholder='请输入'></div>",
                    btn: ["确定", "取消"],
                    btnFun: [callBack1 && function(id, el) {
                        var val = el.querySelector("input").value;
                        return callBack1(val);
                    }, callBack2 && function() {
                        return callBack2();
                    }]
                }, "prompt");
            },
            loading: function(msg) {
                return this.open({
                    content: "<div class='type_load'></div><span class='msg'>" + msg + "</span>"
                }, "loading");
            },
            open: function(opt, type) {
                if (!typeof opt == 'object') {
                    return
                }
                var btns = "";
                var event = [];
                if (opt.btnFun && opt.btnFun.length && opt.btnFun.length > 0) {
                    event = opt.btnFun;
                };
                opt.btn && each(opt.btn, function(index, item) {
                    btns += "<button " + (index == (opt.btn.length - 1) ? "class='dialog_btn yes'" : "class='dialog_btn'") + " ids='id_" + index + "'>" + item + "</button>";
                })
                return _that.show({
                    template: "<div class='content_" + type + "'>" + opt.content + "</div>" + (btns && "<div class='btns " + type + "'>" + btns + "</div>" || "")
                }, event);

            },
            close: function(id) {
                _that.close(id);
            },
            clickBtn: function(e) {
                var id = this.getAttribute("id").substr(2);
                var el = this;
                /**检测是否点击了按钮**/
                if (_that.dialog.contains(e.target)) {
                    if (e.target.getAttribute("class") && e.target.getAttribute("class").indexOf("dialog_btn") > -1) {
                        if (e.target.parentNode) {
                            var childNodes = e.target.parentNode.children;
                            each(childNodes, function(i, item) {
                                if (item == e.target) {
                                    if (btnCallBack[id][i]) {
                                        var result = btnCallBack[id][i](id, el);
                                        if (result == true || typeof result == "undefined") {
                                            btnCallBack[id].splice(i, 1);
                                            _that.close(id);
                                            /**清除已执行的回调函数**/
                                        };
                                    } else {
                                        btnCallBack[id].splice(i, 1);
                                        _that.close(id);

                                    }

                                }
                            })
                        }

                    }
                };

            }
        }
    }
    /**主类 core**/
    function Class(config) {
        this.dialog = null;
        return init.call(this);
    }

    Class.prototype.template = function(body, dialogId) {

        return '<div class="push_dialog">' + body + '</div>';
    }

    Class.prototype.addEvent = function(el) {
        el.removeEventListener("click", Dialog.clickBtn, true);
        el.addEventListener("click", Dialog.clickBtn, true);
    }
    Class.prototype.success = function(el, id, event) {
        this.resize(el);
        btnCallBack[id] = event;
    }
    Class.prototype.resize = function(el) {
        var content = el.children[1];
        var mask = el.children[0];
        var contentClass = content.getAttribute("class");
        var offset = {
            width: content.offsetWidth,
            height: content.offsetHeight,
            clientWidth: document.documentElement.clientWidth || window.innerWidth,
            clientHeight: document.documentElement.clientHeight || window.innerHeight
        }
        content.style.cssText = "left:" + (offset.clientWidth - offset.width) / 2 / offset.clientWidth * 100 + "%;top:" + (offset.clientHeight - offset.height) / 2 / offset.clientHeight * 100 + "%;";
        content.setAttribute("class", contentClass + " ani");
        mask.className += " ani";
        this.addEvent(el);
    }
    Class.prototype.show = function(obj, event) {
        var htmstr = obj.template || "";
        var mask = "<div class='push_mask'></div>";
        var div = document.createElement("div");
        div.setAttribute("class", "push_dialog_content");
        var dialogId = PushDialogId.length == 0 ? defaultId : PushDialogId[PushDialogId.length - 1] + 1;
        div.style.cssText = "position:fixed;left:0px;right:0px;top:0px;bottom:0px;z-index:" + dialogId;
        div.setAttribute("id", "d_" + dialogId);
        div.innerHTML = mask + this.template(htmstr, dialogId);
        document.body.appendChild(div);
        PushDialogId.push(dialogId);
        this.dialog = div;
        this.success(div, dialogId, event); //执行内部打开逻辑
        if (typeof obj.success == "function") {
            obj.success(div, dialogId);
        }
        return dialogId;
    }
    Class.prototype.close = function(ids) {
        var dialogId = null;
        if (ids && PushDialogId.indexOf(Number(ids)) > -1) {
            dialogId = PushDialogId.splice(PushDialogId.indexOf(Number(ids)), 1);
        } else {
            dialogId = PushDialogId.pop();
        }
        delete btnCallBack[dialogId];
        var dialog = document.getElementById("d_" + dialogId);
        dialog && dialog.removeEventListener("click", Dialog.clickBtn);
        dialog && document.body.removeChild(dialog);
        this.dialog = document.getElementById("d_" + PushDialogId[PushDialogId.length - 1]);
    }
    if (window.PushDialog) {
        return console.warn('命名空间重复');
    } else {
        if (typeof window.vgui == "object") {
            window.vgui.PushDialog = Dialog = new Class();
        } else {
            window.vgui = {};
            vgui.PushDialog = Dialog = new Class();
        }
    }
}(window, document);